// 登录页面
<template>
  <div class="login-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="@/assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 登录的表单 -->
      <!-- element上面 -->
      <el-form :model="loginInfo"  :rules="loginRules"  ref="loginInfo">
        <el-form-item  prop="username">
          <el-input  prefix-icon="el-icon-user" v-model="loginInfo.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input  prefix-icon="el-icon-lock" v-model="loginInfo.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn-login" @click='sbumitFrom' type="primary">登录</el-button>
          <el-link type="primary" @click="$router.push('/reg')">去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import axios from 'axios'

import { reqLogin } from '@/api/user.js'

export default {
  data () {
    return {
      // 登录信息
      loginInfo: {
        username: 'xianyu',
        password: '31415926'
      },
      // 输入框验证规则
      loginRules: {
        // 验证账号
        username: [
          { required: true, message: '账号能不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[0-9a-zA-Z]{1,10}$/, message: '账号名只能由1-10位的字母和数字组成', trigger: ['change', 'blur'] }
        ],
        // 验证密码
        password: [
          { required: true, message: '密码能不能为空', trigger: ['change', 'blur'] },
          { pattern: /^[\S]{6,15}$/, message: '密码长度必须为6-15,且不能有空格', trigger: ['change', 'blur'] }

        ]
      }

    }
  },
  // 函数
  methods: {
    // 验证表单
    sbumitFrom () {
      // 表单验证
      this.$refs.loginInfo.validate(async (flag) => {
        // 表单验证失败提示错误
        if (!flag) return this.$message.error('账号密码验证失败,请重新输入')
        // 验证成功,发送axios请求
        const data = this.loginInfo
        // const { data: res } = await axios.post('http://www.liulongbin.top:3008/api/login', data)
        // 使用封装的方法
        const { data: res } = await reqLogin(data)

        console.log(res)
        // 如果验证失败,提示用户
        if (res.code !== 0) return this.$message.error('账户密码不正确,请重新登录')
        // 如果登录成功,添加token秘钥,跳转到首页
        localStorage.setItem('token', res.token)

        this.$router.push('/')

        this.$message.success('恭喜您,登录成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  background-color: blue;
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    // height: 270px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
      background: url('../../assets/images/login_title.png') no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      font-size: 12px;
    }
  }
}
</style>
